package com.attendance.sz.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class DeviceActionBase {
	
	public final static String header="55aa";
	public final static String tail="aa55";	
	public final static String defaultSN="00000000000000000000";
	
	public final static String deviceCMD1="00";
	public final static String deviceCMD2="01";
	
	public final static String cardCMD1="10";
	public final static String cardCMD2_1="01";//查询用户卡信息
	public final static String cardCMD2_2="02";//改写用户卡信息
	public final static String cardCMD2_3="03";//寻卡
	public final static String cardCMD2_4="04";//钱包充值扣费
	
	public final static String deviceTag_1="01";//设备类型
	public final static String deviceTag_2="02";//设备名称
	public final static String deviceTag_3="03";//设备地址
	
	
	public final static String cardTag_1="01";//物理卡号
	public final static String cardTag_2="02";//卡流水号
	
	public final static String androidAddress="0010";
	public final static String moAddress="0020";
	
	public final static String stat_1="00";//成功
	public final static String stat_2="01";//重试
	public final static String stat_3="02";//没有放卡
	
	public final static int header_size=4;
	public final static int len_size=2;
	public final static int src_size=4;
	public final static int des_size=4;
	public final static int defaultsn_size=20;
	public final static int cm1_size=4;
	public final static int cm2_size=2;
	public final static int stat_size=2;
	public final static int crc_size=4;
	public final static int tail_size=4;
	
	public final static int tag_size=2;
	public final static int taglen_size=2;
	
	
	
	public static String Get16Len(int len)
	{
		return Integer.toHexString(len).toLowerCase();
	}
	
	public static Long Get10Value(String value)
	{
		return Long.parseLong(value, 16);
	}
	
	public static String Get16LineNo(String value,int length)
	{
		String longStr=Long.toHexString(Long.parseLong(value));
		
		int len=longStr.length();
		String strFlag="";
		if(len<length)
		{	
			for(int i=0;i<length-len;i++)
			{
				strFlag=strFlag+"0";
			}
					
		}		
		return (strFlag+longStr).toLowerCase();
	}
	
	public static String CreateInputValue(String header,int len,String srcAddr,String desAddr
			,String defaultSN,String cm1,String cm2,String tag,String tail)
	{
		String len16=Get16Len(len);
		String inputValue=len16+srcAddr+desAddr+defaultSN+cm1+cm2+tag;
		String crc16=ClsPublic.CalculateStringCRC16(len16+srcAddr+desAddr+defaultSN+cm1+cm2+tag);
		inputValue=header+inputValue+crc16+tail;
		return inputValue.toLowerCase();
	}
	
	public static String GetChineseFromGB2312(String str16)
	{
		try {
			byte[] strByte=ClsPublic.HexString2Bytes(str16);
			return new String(strByte,"gb2312").trim();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	public static String GetCrc16FromOutput(String output)
	{
		return output.substring(output.indexOf(tail)-crc_size,output.indexOf(tail));
	}
	
	public static String GetCreateCrc16StringFromOutput(String output)
	{
		return output.substring(header_size,output.indexOf(tail)-crc_size);
	}

	public static String GetCommandFromOutput(String output)
	{
		return output.substring(header_size+len_size
				,header_size+len_size+cm1_size);
	}
	
	public static String GetStatusFromOutput(String output)
	{
		return output.substring(header_size+len_size+cm1_size
				,header_size+len_size+cm1_size+stat_size);
	}
	
	public static String GetTagFromOutput(String output)
	{
//		String strLen=output.substring(header_size,header_size+len_size);
//		int len=Integer.valueOf(String.valueOf(Get10Value(strLen)));
//		output=output.substring(header_size+len_size,2*len);
		String strTag = output.substring(header_size+len_size+cm1_size+stat_size
				,output.length()-tail_size-crc_size); 
		return strTag;
				
	}
	
	//低字节在前
	public static String Get16CardNoOrLineNo(String value)
	{
		byte[] tempBtye1=ClsPublic.HexString2Bytes(value);
		byte[] tempBtye2=new byte[tempBtye1.length];
		
		int j=0;
		for(int i=tempBtye1.length-1;i>=0;i--)
		{
			tempBtye2[j]=tempBtye1[i];
			j++;
		}
		
		return ClsPublic.Bytes2HexString(tempBtye2).toLowerCase();
	}
	
	public static String GetCardNoOrLineNo(String value)
	{
		if(value.isEmpty()){
			return "";
		}
		byte[] tempBtye1=ClsPublic.HexString2Bytes(value);
		byte[] tempBtye2=new byte[tempBtye1.length];
		
		int j=0;
		for(int i=tempBtye1.length-1;i>=0;i--)
		{
			tempBtye2[j]=tempBtye1[i];
			j++;
		}
		
		return Long.toString(Long.parseLong(ClsPublic.Bytes2HexString(tempBtye2), 16)).toLowerCase();
	}
	
	
	public static String GetStudentNo(String value)
	{
		value=value.toLowerCase();
		if(value.endsWith("f"))
		{
			return value.substring(0, value.length()-1);
		}
		
		return value;
	}
	
	public static String GetCardNo(String value)
	{
		value=value.toLowerCase();
		if(value.endsWith("a"))
		{
			return value.substring(0, value.length()-1)+"x";
		}
		
		return value;
	}
	
	
	
	public static TagEntity GetValueByTay(List<TagEntity> list,String tag)
	{
		if(list==null)return null;
		for(TagEntity te:list)
		{
			if(te.tag.equals(tag))
			{
				return te;
			}
		}
		return null;
	}
	
	public List<TagEntity> GetValueFromOutput(List<TagEntity> list,String tagValue)
	{
		String tag=tagValue.substring(0, tag_size);
		String strLen=tagValue.substring(tag_size, tag_size+taglen_size);
		long len=Get10Value(strLen);
		int intLen=(int)len;
		
		byte[] byteValue=ClsPublic.HexString2Bytes(tagValue);
		int byteFlag=2;
		byte[] tempByte=new byte[intLen];
		for(int i=0;i<intLen;i++)
		{
			tempByte[i]=byteValue[byteFlag];
			byteFlag++;
		}
		String value=ClsPublic.Bytes2HexString(tempByte);
		
//		String value=tagValue.substring(tag_size+taglen_size,2*intLen);
		
		TagEntity te=new TagEntity();
		te.tag=tag;
		te.len=strLen;
		te.value=value;
		
		list.add(te);
		
		String strPart=tagValue.substring(tag_size+taglen_size+value.length());		
		if(strPart.length()>0)
		{
			return GetValueFromOutput(list,strPart);
		}
		
		return list;
	}
	
	
	
	

}
